package controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import dto.OrderNotifyDto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pojo.*;
import service.MerchantManager;
import service.OrderManager;
import enums.OrderType;
import util.JerseyClient;
import util.JsonUtil;

/**
 * Created by CHENCO7 on 8/12/2017.
 */
@Controller
public class OrderController {
    @Resource(name = "orderManager")
    private OrderManager orderManager;

    @Resource(name = "merchantManager")
    private MerchantManager merchantManager;

    @RequestMapping(value = "addToOrderList", method = RequestMethod.POST, consumes = "application/json")
    @ResponseBody
    public List<OrderItem> addToOrderList(@RequestBody OrderItem item, HttpSession session){
        List<OrderItem> orderList = (List<OrderItem>)session.getAttribute("orderList");
        orderList = orderManager.addOrderItemToSession(item, orderList);
        session.setAttribute("orderList", orderList);
        return orderList;
    }

    @RequestMapping(value = "orderAddress", method = RequestMethod.GET)
    @ResponseBody
    public List<Address> test(HttpSession session){
        Customer customer = (Customer) session.getAttribute("customer");
        return customer.getAddressList();
    }

    @RequestMapping(value = "orderPage", method = RequestMethod.GET)
    public String showOrderDetail(HttpSession session, Model model){
        List<OrderItem> orderList = (List<OrderItem>)session.getAttribute("orderList");
        Customer customer = (Customer) session.getAttribute("customer");
        model.addAttribute("customer", customer);
        model.addAttribute("orderList", orderList);
        return "orderPage";
    }

    @RequestMapping(value = "order", method = RequestMethod.POST)
    @ResponseBody
    public String placeOrder(String merchantId, HttpSession session){
        List<OrderItem> orderList = (List<OrderItem>)session.getAttribute("orderList");
        Customer customer = (Customer) session.getAttribute("customer");
        Merchant merchant = merchantManager.findById(merchantId);

        Order order = new Order();
        order.setCustomer(customer);
        order.setOrderItemList(orderList);
        order.setCreateDate(new Date());
        order.setStatus(OrderType.WAIT.toString());
        order.setDeliveryAddress(customer.getDefaultAddress());
        order.setMerchant(merchant);
        order.setProcessNumber("NO" + String.valueOf(System.currentTimeMillis()));
        if(orderManager.addOrder(order)){
            session.setAttribute("orderList", null);
            OrderNotifyDto orderNotifyDto = new OrderNotifyDto(merchantId, order.getProcessNumber());
            JerseyClient jersey = new JerseyClient();
            jersey.post("http://zha-ita077-w7:8080/spring-merchant/api/orders", JsonUtil.toJsonString(orderNotifyDto));
            return order.getId();
        }else {
            return "fail";
        }
    }

    @RequestMapping(value = "comment")
    @ResponseBody
    public String comment(Order order, Comment comment){
        order.getComment().add(comment);
        Order o = orderManager.updateOrder(order);
        if(o != null){
            return "success";
        }else {
            return "error";
        }
    }

    @RequestMapping(value = "orderItem", method = RequestMethod.GET)
    public String showOrderItem(String orderId, Model model){
        Order order = orderManager.loadOrderItem(orderId);
        model.addAttribute("order", order);
        model.addAttribute("orderItemList", order.getOrderItemList());
        System.out.println(order.getOrderItemList().size());
        return "orderItem";
    }

    @RequestMapping(value = "showOrders", method = RequestMethod.GET)
    @ResponseBody
    public List<Order> showOrders(HttpSession session, Model model){
        Customer customer = (Customer) session.getAttribute("customer");
        if(customer == null){
            return null;
        }else {
            List<Order> orders = orderManager.loadOrderList(customer.getId());
            return orders;
        }
    }

    @ResponseBody
    @RequestMapping(value = "comment", method = RequestMethod.POST)
    public String addComment(String comment, String starCount, String o_id){
    	orderManager.addComment(comment, starCount, o_id);
        return "comment ok....";
    }

    @ResponseBody
    @RequestMapping(value = "complain", method = RequestMethod.POST)
    public String addComplain(String complain, String o_id){
    	orderManager.addComplain(complain, o_id);
        return "complian ok....";
    }

    @RequestMapping(value = "determineReceipt", method = RequestMethod.POST)
    @ResponseBody
    public String determineReceipt(String orderId){
        Order order = orderManager.determineReceipt(orderId);
        if(order != null){
            return "success";
        }else {
            return "fail";
        }
    }
}
